//
//  PhotosManager.m
//  OnionHome
//
//  Created by fengjie on 2019/9/17.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PhotosManager.h"
#import "AlbumViewController.h"
#import "PhotoManger.h"

@implementation PhotosManager

+(void)showPhotosManager:(UIViewController *)viewController withMaxImageCount:(NSInteger)maxCount withAlbumArray:(void(^)(NSMutableArray<PhotoModel *> *albumArray))albumArray withCameraImage:(void(^)(UIImage *cameraImage))cameraImage{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (status == PHAuthorizationStatusAuthorized) {
                AlbumViewController *controller = [[AlbumViewController alloc] init];
                controller.confirmAction = ^{
                    albumArray([PhotoManger standardPhotoManger].photoModelList);
                };
                if (![Utilis isSimuLator]) {
                    controller.cameraController.rentunBlock = ^(UIImage * _Nonnull image) {
                        cameraImage(image);
                    };
                }
                UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
                
                [PhotoManger standardPhotoManger].maxCount = maxCount;
                
                [viewController presentViewController:navigationController animated:YES completion:nil];
            }else{
                FJAlertController *alertViewController = [FJAlertController alertControllerWithTitle:@"访问相册" message:@"您还没有打开相册权限" preferredStyle:UIAlertControllerStyleAlert];
                
                FJAlertAction *action1 = [FJAlertAction actionWithTitle:@"去打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    if ([[UIApplication sharedApplication] canOpenURL:url]) {
                        [[UIApplication sharedApplication] openURL:url];
                    }
                }];
                FJAlertAction *action2 = [FJAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    DLog(@"点击了取消");
                }];
                
                [alertViewController addAction:action1];
                [alertViewController addAction:action2];
                action1.textColor = UIColorFromRGB(0x009966);
                action2.textColor = UIColorFromRGB(0x009966);
                [viewController presentViewController:alertViewController animated:YES completion:nil];
            }
        });
    }];
}
@end
